package com.futures.investm.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.futures.investm.R;
import com.futures.investm.adapter.HandPickAdapter;
import com.futures.investm.constant.Api;
import com.futures.investm.entity.HandpickEntity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;

import java.util.ArrayList;

import tech.com.commoncore.base.BaseFragment;
import tech.com.commoncore.entity.BaseTResp;


public class HandPickFragment extends BaseFragment {

    private RecyclerView mRecyclerview;

    HandPickAdapter handPickAdapter;


    private SmartRefreshLayout mSmartLayoutRootFastLib;

    private View loadingView;
    private int currentPage = 0;

    int i = 0;

    /**
     * fragment静态传值
     */
    public static HandPickFragment newInstance(int i) {
//        ContentFragment fragment = new ContentFragment();
//        Bundle bundle = new Bundle();
//        bundle.putInt(key, i);
//        fragment.setArguments(bundle);
        Bundle args = new Bundle();
        args.putInt("i",i);
        HandPickFragment fragment = new HandPickFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_content;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        i = getArguments().getInt("i");


        mSmartLayoutRootFastLib = (SmartRefreshLayout) mContentView.findViewById(R.id.smartLayout_rootFastLib);
        mRecyclerview = (RecyclerView) mContentView.findViewById(R.id.recyclerview);
        loadingView = mContentView.findViewById(R.id.loadingview);

        mRecyclerview.setLayoutManager(new LinearLayoutManager(mContext));

//        if (i == 0){
//            coinFlashAdapter = new CoinFlashAdapter(R.layout.item_coin_flash);
//            mRecyclerview.setAdapter(coinFlashAdapter);
//            requestFlashDatas();
//        }else if (i == 1){
            handPickAdapter = new HandPickAdapter(R.layout.item_article);
            mRecyclerview.setAdapter(handPickAdapter);
            getHandPick();
//        }


        mSmartLayoutRootFastLib.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
//                if (i == 0) {
//                    requestFlashDatas();
//                } else if (i == 1){
                    getHandPick();
//                }

            }
        });

        mSmartLayoutRootFastLib.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                currentPage = 0;
//                if (i == 0) {
//                    requestFlashDatas();
//                } else if (i == 1){
                    getHandPick();
//                }

            }
        });


    }

    private void getHandPick() {
        ViseHttp.GET(Api.CURRENCY_NEWS)
                .baseUrl(Api.BASE_URL)
                .addParam("page", ++currentPage + "")
                .addParam("type", "media")
                .request(new ACallback<BaseTResp<ArrayList<HandpickEntity>>>() {
                    @Override
                    public void onSuccess(BaseTResp<ArrayList<HandpickEntity>> datas) {
                        if (currentPage == 1) {
                            loadingView.setVisibility(View.GONE);
                            mSmartLayoutRootFastLib.setVisibility(View.VISIBLE);
                            mSmartLayoutRootFastLib.finishRefresh();
                        } else {
                            mSmartLayoutRootFastLib.finishLoadMore();
                        }
                        if (datas.code == 1) {
                            if (currentPage == 1) {
                                handPickAdapter.replaceData(datas.data);
                            } else {
                                handPickAdapter.addData(datas.data);
                            }
                        } else {
                            //数据为空
                            if (currentPage == 1) {
//                                coinFlashAdapter.setEmptyView(R.layout.layout_empty,mRecyclerview);
                            }
                            currentPage--;
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        if (currentPage == 1) {
                            loadingView.setVisibility(View.GONE);
                            mSmartLayoutRootFastLib.setVisibility(View.VISIBLE);
                            mSmartLayoutRootFastLib.finishRefresh();
                        } else {
                            mSmartLayoutRootFastLib.finishLoadMore();
                        }
                        currentPage--;
//                        coinFlashAdapter.setEmptyView(R.layout.layout_empty,mRecyclerview);
                    }
                });
    }


}